package org.dromara.system.controller.system;

import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.dromara.system.domain.bo.StageActTaskBo;
import org.dromara.system.domain.bo.SysUserMachinePolicyBo;
import org.dromara.system.domain.vo.StageActTaskVo;
import org.dromara.system.domain.vo.SysUserMachinePolicyVo;
import org.dromara.system.service.ISysUserMachinePolicyService;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 机具政策
 *
 * @author Lion Li
 * @date 2025-07-10
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/userMachinePolicy")
public class SysUserMachinePolicyController extends BaseController {

    private final ISysUserMachinePolicyService sysUserMachinePolicyService;

    /**
     * 查询机具政策列表
     */
//    @SaCheckPermission("system:userMachinePolicy:list")
    @GetMapping("/list")
    public TableDataInfo<SysUserMachinePolicyVo> list(SysUserMachinePolicyBo bo, PageQuery pageQuery) {
        return sysUserMachinePolicyService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出机具政策列表
     */
//    @SaCheckPermission("system:userMachinePolicy:export")
    @Log(title = "机具政策", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(SysUserMachinePolicyBo bo, HttpServletResponse response) {
        List<SysUserMachinePolicyVo> list = sysUserMachinePolicyService.queryList(bo);
        ExcelUtil.exportExcel(list, "机具政策", SysUserMachinePolicyVo.class, response);
    }

    /**
     * 获取机具政策详细信息
     *
     * @param id 主键
     */
//    @SaCheckPermission("system:userMachinePolicy:query")
    @GetMapping("/{id}")
    public R<SysUserMachinePolicyVo> getInfo(@NotNull(message = "主键不能为空")
                                             @PathVariable Long id) {
        return R.ok(sysUserMachinePolicyService.queryById(id));
    }

    /**
     * 新增机具政策
     */
//    @SaCheckPermission("system:userMachinePolicy:add")
    @Log(title = "机具政策", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@RequestBody @Validated SysUserMachinePolicyBo bo) {
        return toAjax(sysUserMachinePolicyService.insertByBo(bo));
    }

    /**
     * 修改机具政策
     */
//    @SaCheckPermission("system:userMachinePolicy:edit")
    @Log(title = "机具政策", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated @RequestBody SysUserMachinePolicyBo bo) {
        return toAjax(sysUserMachinePolicyService.updateByBo(bo));
    }

    /**
     * 删除机具政策
     *
     * @param ids 主键串
     */
//    @SaCheckPermission("system:userMachinePolicy:remove")
    @Log(title = "机具政策", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(sysUserMachinePolicyService.deleteWithValidByIds(List.of(ids), true));
    }

    //    @SaCheckPermission("system:userMachinePolicy:stageActTask")
    @Log(title = "阶段返现详情", businessType = BusinessType.OTHER)
    @PostMapping("/stageActTask")
    public R<StageActTaskVo> stageActTask(@RequestBody @Validated StageActTaskBo bo) {
        return R.ok(sysUserMachinePolicyService.stageActTask(bo));
    }


    @Log(title = "执行机器达标返现", businessType = BusinessType.OTHER)
    @PostMapping("/machineCompleteCashBack")
    @Scheduled(cron = "0 0 0 * * ?") // 每天0点执行
    public void machineCompleteCashBack() {
        sysUserMachinePolicyService.machineCompleteCashBack();
    }

    @Log(title = "执行机器阶段返现", businessType = BusinessType.OTHER)
    @PostMapping("/machineStageCashBack")
    @Scheduled(cron = "0 0 0 1 * ?") // 每月1号零点执行
    public void machineStageCashBack() {
        sysUserMachinePolicyService.machineStageCashBack();
    }


    @GetMapping("/dealCompleteCashBack/{sn}")
    public void dealCompleteCashBack(@PathVariable String sn) {
        sysUserMachinePolicyService.dealCompleteCashBack(sn);
    }

    @GetMapping("/dealStageCashBack/{sn}")
    public void dealStageCashBack(@PathVariable String sn) {
        sysUserMachinePolicyService.dealStageCashBack(sn);
    }

}
